import VueRouter from "vue-router";
import FrontIndex from "@/components/easyfront/FrontIndex.vue";
import BackIndex from '@/components/backend/BackIndex';
import BackInfo from '@/components/backend/news/BackNews';
import NewsAdd from '@/components/backend/news/NewsAdd';
import NewsEdit from '@/components/backend/news/NewsEdit';
import BackProducts from "@/components/backend/products/BackProducts.vue";
import ProductsAdd from "@/components/backend/products/ProductsAdd.vue";
import ProductsEdit from "@/components/backend/products/ProductsEdit.vue";
import Regist from '@/components/easyfront/regist/Regist.vue'
import Login from "@/components/easyfront/Login.vue";
import Vue from "vue";
import NewsDetail from "@/components/backend/news/NewsDetail.vue";
import GoodsDetail from "@/components/easyfront/products/GoodsDetail.vue";
import GoodsList from "@/components/easyfront/products/GoodsList.vue";
import BackOrders from '@/components/backend/order/BackOrder.vue';

import BackCollects from "@/components/backend/collects/BackCollects.vue";
import CartOne from "@/components/easyfront/cart/CartOne.vue";
import CartTwo from "@/components/easyfront/cart/CartTwo.vue";
import CartThree from "@/components/easyfront/cart/CartThree.vue";
import BackOrderPro from '@/components/backend/order/BackOrderPro.vue';
import UserList from '@/components/backend/user/UserList.vue';
import AddressList from '@/components/backend/address/Addresslist.vue';
import Categorylist from '@/components/backend/category/Categorylist';
import ShowCode from "@/components/easyfront/cart/ShowCode.vue";
const router =  new VueRouter({
    scrollBehavior(to,from,savadPosition){
        if(savadPosition){
            return savadPosition;
        } else {
            return { x: 0, y: 0 }
        }
    },
    routes: [
        {
            path: '/',
            redirect: '/front-index'
        },
        {
            path: '/goodslist',
            name: 'GoodsList',
            component: GoodsList,
            meta: { title: '商品列表', isAuth: false }
        },
        {
            path: '/detail',
            name: 'GoodsDetail',
            component: GoodsDetail,
            meta: { title: '商品详情', isAuth: false }
        },
        {
            path: '/cart-one',
            name: 'CartOne',
            component: CartOne,
            meta: { title: '查看购物车', isAuth: true, permission: [0, 1] }
        },
        {
            path: '/cart-two',
            name: 'CartTwo',
            component: CartTwo,
            meta: { title: '确认订单信息', isAuth: true, permission: [0, 1] }
        },
        {
            path: '/cart-three',
            name: 'CartThree',
            component: CartThree,
            meta:{title:'订单',isAuth:true,permission:[0,1]}
        },
        {
            path:'/show-code',
            name:'ShowCode',
            component:ShowCode,
            meta: { title: '支付', isAuth: true, permission: [0, 1] }
        },
        {
            path: '/front-index',
            name: 'FrontIndex',
            component: FrontIndex,
            meta: { title: '首页', isAuth: false },

        },
        {
            path: '/login',
            name: 'Login',
            component: Login,
            meta: { title: '登录', isAuth: false }
        },
        {
            path: '/regist',
            name: 'Regist',
            component: Regist,
            meta: { title: '注册', isAuth: false }
        },
        {
            path: '/back-index',
            name: 'BackIndex',
            component: BackIndex,
            children: [

                {
                    path: 'back-collects',
                    name: 'BackCollects',
                    component: BackCollects,
                    meta: { title: '我的收藏', isAuth: true, permission: [0, 1, 2] }
                },
                {
                    path: 'back-orders',
                    name: 'BackOrders',
                    component: BackOrders,
                    meta: { title: '订单管理', isAuth: true, permission: [0, 1] },
                },
                {
                    path:'back-BackOrderPro',
                    name:'BackOrderPro',
                    component:BackOrderPro,
                    meta:{title:'订单下的商品',isAuth:true,permission:[0,1]},
                },
                {
                    path: 'back-news',
                    name: 'BackNews',
                    component: BackInfo,
                    meta: { title: '资讯管理', isAuth: true, permission: [0, 1, 2] }

                },
                {

                    path: 'add-news',
                    name: 'NewsAdd',
                    component: NewsAdd,
                    meta: { title: '添加资讯', isAuth: true, permission: [1] }
                },
                {
                    path: 'edit-news/:id/:title/:content',
                    name: 'NewsEdit',
                    component: NewsEdit,
                    meta: { title: '修改资讯', isAuth: true, permission: [1] }
                },
                {
                    path: 'news-detail',
                    name: 'NewsDetail',
                    component: NewsDetail,
                    meta: { title: '资讯详情', isAuth: false }
                },
                {
                    path: 'back-products',
                    name: 'BackProducts',
                    component: BackProducts,
                    meta: { title: '商品管理', isAuth: true, permission: [1] }
                },
                {
                    path: 'add-products',
                    name: 'ProductsAdd',
                    component: ProductsAdd,
                    meta: { title: '添加商品', isAuth: true, permission: [1] }
                },
                {
                    path: 'edit-products',
                    name: 'ProductsEdit',
                    component: ProductsEdit,
                    meta: { title: '修改商品', isAuth: true, permission: [1] }
                },
                {
                    path: 'show-address',
                    name: 'AddressList',
                    component: AddressList,
                    meta: { title: '显示所有地址信息', isAuth: true, permission: [1,0] }
                },
                {
                    path: 'show-user',
                    name: 'UserList',
                    component: UserList,
                    meta: { title: '显示所有用户信息', isAuth: true, permission: [1,0] }
                },
                {
                    path: 'show-category',
                    name: 'Categorylist',
                    component: Categorylist,
                    meta: { title: '显示所有分类信息', isAuth: true, permission: [1] }
                }
            ]
        },
    ]
})
//全局前置路由，在初始化时会调用，在每次路由切换之前会调用
router.beforeEach((to, from, next) => {
    if (!to.meta.isAuth) {
        //去到的页面不需要进行登录校验
        next();
        return;
    }
    let token = window.sessionStorage.getItem("token");
    if (!token) {
        Vue.prototype.$message.error('请先登录！');
        next('/login');
        return;
    }
    let userRole = window.sessionStorage.getItem('userRole');
    let arr = to.meta.permission;
    console.log("userRole", userRole, Number.parseInt(userRole));
    if (arr.indexOf(Number.parseInt(userRole)) != -1) {
        //如果用户登录,且有权限
        next();
        return;
    } else {
        Vue.prototype.$message.error('权限不足');
        return;
    }
})
//全局后置路由守卫
router.afterEach((to, from) => {
    document.title = to.meta.title;
})
export default router;